package com.waiting.https;

import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/1/18
 **/
public class Main {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
//
//        String url = "https://www.baidu.com/";
//        try (CloseableHttpClient httpClient = createHttpClient()) {
//            HttpGet httpGet = new HttpGet(url);
//            try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
//                HttpEntity entity = httpResponse.getEntity();
//                String result = EntityUtils.toString(entity);
//                EntityUtils.consume(entity);
//
//                System.out.printf(result);
//            }
//        }
        StringBuilder emailContent = new StringBuilder("");
        System.out.println(emailContent.toString());
    }


    private static CloseableHttpClient createHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
//        SSLContext sslcontext = SSLContexts.custom()
//                .loadTrustMaterial(null, (chain, authType) -> true)
//                .build();
        SSLContext sslcontext = null;
        try {
            sslcontext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new BrowserCompatHostnameVerifier());
//            SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslcontext, null, null,
//                    new NoopHostnameVerifier());
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
